package dev.android.yeqh.tvannounce;

import java.util.List;

import dev.android.yeqh.tvannounce.adapters.ChannelListAdapter;
import dev.android.yeqh.tvannounce.common.Constants;
import dev.android.yeqh.tvannounce.common.NetTask;
import dev.android.yeqh.tvannounce.common.Utils;
import dev.android.yeqh.tvannounce.model.Channel;
import dev.android.yeqh.tvannounce.model.NetTaskParam;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class ChannelsActivity extends BaseActivity {
	ListView lvChannels = null;
	ChannelListAdapter adapter = null;
	
	OnItemClickListener listener = new OnItemClickListener() {

		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
			try {
				Intent intent = new Intent(ChannelsActivity.this, SubChannelsActivity.class);
				intent.putExtra("station_id", adapter.getChannelId(arg2));
				ChannelsActivity.this.startActivity(intent);
			} catch (Exception e) {
				Utils.showMessage(ChannelsActivity.this, e);
			}
		}
	};
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.channel_list);
        
        lvChannels = (ListView)findViewById(R.id.channel_list_list);
        
        try {
        	new NetTask(this).execute(new NetTaskParam(Constants.NET_TASK_GET_CHANNELS, getIntent().getStringExtra("area_name")));
        	adapter = new ChannelListAdapter(null, this, R.layout.item_channel);

        	lvChannels.setAdapter(adapter);
        	lvChannels.setOnItemClickListener(listener);
        } catch (Exception e) {
			Utils.showMessage(this, e);
		}
    }
    
    @SuppressWarnings("unchecked")
	@Override
    public void updateUI(Object result) {
    	try {
	    	adapter.setChannels((List<Channel>)result);
	    	adapter.notifyDataSetChanged();
    	} catch (Exception e) {
		}
    }
}